package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.PhoneNumCheck;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.utils.ShowToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class PhoneNumberCheckActivity extends TitleBarActivity implements View.OnClickListener {

    private TextView tvConfirm, tvGetCode;
    private EditText etPhoneNumber, etCheckCode;
    private TimeCount time;
    private String checkCode="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_number_check);
        setTitleBarText("验证手机号码");

        initView();
        time = new TimeCount(60000, 1000);
    }

    private void initView() {
        tvConfirm= (TextView) findViewById(R.id.tv_check_phone_confirm);
        tvGetCode= (TextView) findViewById(R.id.tv_check_phone_getCode);
        etPhoneNumber= (EditText) findViewById(R.id.et_check_phone_phoneNum);
        etCheckCode= (EditText) findViewById(R.id.et_check_phone_checkCode);

        tvConfirm.setOnClickListener(this);
        tvGetCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_check_phone_confirm:
                if (etPhoneNumber.getText().toString()==null||"".equals(etPhoneNumber.getText().toString())) {
                    ShowToast.shortTime("请输入手机号码");
                }
                else if (!PhoneNumCheck.isMobileNo(etPhoneNumber.getText().toString())) {
                    ShowToast.shortTime("请输入正确的手机号码");
                }
                else if (etCheckCode.getText().toString()==null||"".equals(etCheckCode.getText().toString())) {
                    ShowToast.shortTime("请输入验证码");
                }
                else if (etCheckCode.getText().toString().length()!=4) {
                    ShowToast.shortTime("请输入四位长度的验证码");
                }
                else {
                    Map<String, String> params = new HashMap<>();
                    params.put("phone", etPhoneNumber.getText().toString());
                    params.put("userId", SPUtil.getString(SPConstants.USER_ID));
                    params.put("verifyCode", etCheckCode.getText().toString());
                    putJsonEnqueue(UrlConstants.MESSAGE_VERIFY, params, new OkHttpUtil.NetCallBack() {
                        @Override
                        public void onSucceed(String json) {
                            JSONObject jobj=null;
                            try {
                                jobj = new JSONObject(json);
                                String verify = jobj.getString("verifyMatch");
                                if ("1".equals(verify)) {
                                    //验证成功
                                    ShowToast.shortTime("验证成功");
                                    Intent intent = new Intent();
                                    intent.putExtra("phoneNumber", etPhoneNumber.getText().toString());
                                    setResult(1001, intent);
                                    finish();
                                }
                                else {
                                    //验证失败
                                    ShowToast.shortTime("验证失败");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailed(String reason) {
                            ShowToast.shortTime(reason);
                        }
                    });
                }
                break;
            case R.id.tv_check_phone_getCode:
                if (etPhoneNumber.getText().toString()==null||"".equals(etPhoneNumber.getText().toString())) {
                    ShowToast.shortTime("请输入手机号码");
                }
                else if (!PhoneNumCheck.isMobileNo(etPhoneNumber.getText().toString())) {
                    ShowToast.shortTime("请输入正确的手机号码");
                }
                else{
                    Map<String, String> params = new HashMap<>();
                    params.put("phone", etPhoneNumber.getText().toString());
                    params.put("userId", SPUtil.getString(SPConstants.USER_ID));
                    params.put("verifyCode", "");
                    putJsonEnqueue(UrlConstants.MESSAGE_VERIFY, params, new OkHttpUtil.NetCallBack() {
                        @Override
                        public void onSucceed(String json) {
                            JSONObject jobj=null;
                            try {
                                jobj = new JSONObject(json);
                                String message = jobj.getString("verifyMessage");
                                ShowToast.shortTime(message);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailed(String reason) {
                            ShowToast.shortTime(reason);
                        }
                    });
                    time.start();
                }
                break;
        }
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setEnabled(false);
            tvGetCode.setText(millisUntilFinished / 1000 + "秒");
            tvGetCode.setBackgroundResource(R.drawable.sl_bg_gray1_gray2_corner4);
        }

        @Override
        public void onFinish() {
            tvGetCode.setText("重新发送");
            tvGetCode.setEnabled(true);
            tvGetCode.setBackgroundResource(R.drawable.sl_bg_blue1_blue2_corner4);
        }
    }
}
